# -*- coding:utf-8 -*-

from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from common.my_file import MyFile
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver
import platform
from selenium.webdriver.chrome.service import Service

'''
北交所监管数据
'''




class WebdriverBase:
    driver = ""
    __log_file = ""
    db = ""
    __all_cnt = 0
    __err_cnt = 0
    def __init__(self, file_name):
        self.db = MysqlHander("config.ini")
        self.__log_file = open(file_name, 'a', encoding = 'utf-8')

        chromedriver = ""

        options = webdriver.ChromeOptions()
        if platform.system() == "Linux":
            chromedriver = "/usr/bin/chromedriver"
            options.add_argument('--headless')
            options.add_argument('--no-sandbox')
            options.add_argument('--disable-dev-shm-usage')
        else:
            chromedriver = "D:\\chromedriver.exe"
        os.environ["webdriver.chrome.driver"] = chromedriver

        self.ser = Service("/usr/bin/chromedriver")
        self.ser.command_line_args()
        self.ser.start()

        self.driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = options)

        MyFile.wrtie_log(self.__log_file, "开始")

    def add_query_cnt(self):
        self.__all_cnt = self.__all_cnt + 1

    def add_err_cnt(self):
        self.__err_cnt = self.__err_cnt + 1

    def write_log(self, log):
        MyFile.wrtie_log(self.__log_file, log)

    def __del__(self):
        MyFile.wrtie_log(self.__log_file, "all:[" + str(self.__all_cnt) + "],error[" + str(self.__err_cnt) + "]")
        MyFile.wrtie_log(self.__log_file, "结束")
        self.__log_file.close()
        self.ser.stop()
        pass


